var background_layer,
water_layer,
cup30g_layer,
cup70g_layer,
cup100g_layer,
event_layer,

mask_layer;

var background_2d,
water_2d,
cup30g_2d,
cup70g_2d,
cup100g_2d,
event_2d;

var cup30g,
cup70g,
cup100g;

var reset_btn;

$(document).ready(function(){
	background_layer = $('#background_layer');
	water_layer      = $('#water_layer');
	cup30g_layer     = $('#cup30g_layer');
	cup70g_layer     = $('#cup70g_layer');
	cup100g_layer    = $('#cup100g_layer');
	event_layer      = $('#event_layer');
	mask_layer       = $('#mask_layer');

	background_2d = background_layer.get(0).getContext('2d');
	water_2d      = water_layer.get(0).getContext('2d');
	cup30g_2d     = cup30g_layer.get(0).getContext('2d');
	cup70g_2d     = cup70g_layer.get(0).getContext('2d');
	cup100g_2d    = cup100g_layer.get(0).getContext('2d');
	event_2d      = event_layer.get(0).getContext('2d');

	reset_btn     = $('#reset_btn');

	G.onresize('#js--pour-water-game',940,605);

	var load = new G.Load();
	load.media({
		img : [
			{name : 'background',path : './img/background.png'},

			{name : 'cup30g_z1',path : './img/cup30g_z1.png'},
			{name : 'cup30g_z2',path : './img/cup30g_z2.png'},
			{name : 'cup30g_z3',path : './img/cup30g_z3.png'},
			{name : 'cup30g_z4',path : './img/cup30g_z4.png'},

			{name : 'cup70g_z1',path : './img/cup70g_z1.png'},
			{name : 'cup70g_z2',path : './img/cup70g_z2.png'},
			{name : 'cup70g_z3',path : './img/cup70g_z3.png'},
			{name : 'cup70g_z4',path : './img/cup70g_z4.png'},

			{name : 'cup100g_z1',path : './img/cup100g_z1.png'},
			{name : 'cup100g_z2',path : './img/cup100g_z2.png'},
			{name : 'cup100g_z3',path : './img/cup100g_z3.png'},
			{name : 'cup100g_z4',path : './img/cup100g_z4.png'},

			{name : 'water',path : './img/water.png'}
		],
		audio : [

		]
	},function(progress){
		console.log(progress);
	},gameInit);
});

function gameInit(ret){
	resources = ret;

	var offset1g_30 = 94 / 30;
	var offset1g_70 = 145 / 70;
	var offset1g_100 = 223 / 100;

	cup30g = new Cup({
		type : 'cup30g',
		x : 79,
		y : 360,
		w : 208,
		h : 138,
		graduate : 0,
		fillGraduate : 30,
		waterSx : 98-79,waterSy : 391-360,
		waterSw : 123,waterSh : 94,
		fontSx : 60,fontSy : 100,
		offset1g : offset1g_30,
		render : cup30g_2d
	});
	cup70g = new Cup({
		type : 'cup70g',
		x : 366,
		y : 312,
		w : 207,
		h : 186,
		graduate : 0,
		fillGraduate : 70,
		waterSx : 385-366,waterSy : 343-312,
		waterSw : 123,waterSh : 145,
		fontSx : 60,fontSy : 110,
		offset1g : offset1g_70,
		render : cup70g_2d
	});
	cup100g = new Cup({
		type : 'cup100g',
		x : 652,
		y : 233,
		w : 207,
		h : 265,
		graduate : 100,
		fillGraduate : 100,
		waterSx : 671-652,waterSy : 264-233,
		waterSw : 123,waterSh : 223,
		fontSx : 49,fontSy : 161,
		offset1g : offset1g_100,
		render : cup100g_2d
	});
	/**
	 * 绘制初始游戏
	 */
	renderInit();

	event_layer.mousedown(UserDown);
	event_layer.mousemove(UserMove);
	event_layer.mouseup(UserUp);
	reset_btn.click(function(e){
		cup30g.resetParam({
			x : 79,
			y : 360,
			w : 208,
			h : 138,
			graduate : 0,
			fillGraduate : 30,
			waterSx : 98-79,waterSy : 391-360,
			waterSw : 123,waterSh : 94,
			fontSx : 60,fontSy : 100
		});
		cup70g.resetParam({
			x : 366,
			y : 312,
			w : 207,
			h : 186,
			graduate : 0,
			fillGraduate : 70,
			waterSx : 385-366,waterSy : 343-312,
			waterSw : 123,waterSh : 145,
			fontSx : 60,fontSy : 110
		});
		cup100g.resetParam({
			x : 652,
			y : 233,
			w : 207,
			h : 265,
			graduate : 100,
			fillGraduate : 100,
			waterSx : 671-652,waterSy : 264-233,
			waterSw : 123,waterSh : 223,
			fontSx : 49,fontSy : 161
		});

		renderInit();
	});
}

/**
 * 绘制初始游戏
 */
function renderInit(){
	var b = background_2d,
	c30g = cup30g_2d,
	c70g = cup70g_2d,
	c100g = cup100g_2d;

	b.clearRect(0,0,940,605);
	c30g.clearRect(0,0,940,605);
	c70g.clearRect(0,0,940,605);
	c100g.clearRect(0,0,940,605);

	background_2d.drawImage(resources['background'],0,0,940,605);

	c30g.clearRect(0,0,940,605);
	c30g.save();
	c30g.drawImage(resources['cup30g_z2'],79,360,208,138);
	c30g.drawImage(resources['cup30g_z3'],79,360,208,138);
	c30g.fillStyle = '#ffffff';
	c30g.font='bolder 30px Microsoft YaHei';
	c30g.fillText(cup30g.graduate+'g',79+60,360+100);
	c30g.restore();

	c70g.clearRect(0,0,940,605);
	c70g.save();
	c70g.drawImage(resources['cup70g_z2'],366,312,207,186);
	c70g.drawImage(resources['cup70g_z3'],366,312,207,186);
	c70g.fillStyle = '#ffffff';
	c70g.font='bolder 30px Microsoft YaHei';
	c70g.fillText(cup70g.graduate+'g',366+60,312+110);
	c70g.restore();


	c100g.clearRect(0,0,940,605);
	c100g.save();
	c100g.drawImage(resources['cup100g_z2'],652,233,207,265);
	c100g.fillStyle = '#2a81b9';
	c100g.fillRect(671,264,123,223);
	c100g.drawImage(resources['cup100g_z3'],652,233,207,265);
	c100g.fillStyle = '#ffffff';
	c100g.font='bolder 30px Microsoft YaHei';
	c100g.fillText(cup100g.graduate+'g',652+49,233+161);
	c100g.restore();
}
/**
 * 绘制 30 g 相关动画
 */

/**
 * 鼠标操作事件
 */
function UserDown(e){
	var point = G.event_offset(e,940,605),
	offsetX = point.offsetX,offsetY = point.offsetY,
	point_rect = G.point_rect;

	if(point_rect(offsetX,offsetY,cup30g.x,cup30g.y,cup30g.w,cup30g.h)){

		console.log('30g down');
		// 调整 z-index
		cup30g_layer.css('z-index',4);
		cup70g_layer.css('z-index',3);
		cup100g_layer.css('z-index',2);

		cup30g.down(offsetX,offsetY);

	}else if(point_rect(offsetX,offsetY,cup70g.x,cup70g.y,cup70g.w,cup70g.h)){

		console.log('70g down');
		// 调整 z-index
		cup30g_layer.css('z-index',3);
		cup70g_layer.css('z-index',4);
		cup100g_layer.css('z-index',2);

		cup70g.down(offsetX,offsetY);
		
	}else if(point_rect(offsetX,offsetY,cup100g.x,cup100g.y,cup100g.w,cup100g.h)){

		console.log('100g down');
		// 调整 z-index
		cup30g_layer.css('z-index',3);
		cup70g_layer.css('z-index',2);
		cup100g_layer.css('z-index',4);

		cup100g.down(offsetX,offsetY);

	}
}
function UserMove(e){
	var point = G.event_offset(e,940,605),offsetX = point.offsetX,offsetY = point.offsetY;
	if( cup30g.isMove ){

		console.log('30g move');

		cup30g.move(offsetX,offsetY);

	}else if( cup70g.isMove ){

		console.log('70g move');

		cup70g.move(offsetX,offsetY);

	}else if( cup100g.isMove ){

		console.log('100g move');

		cup100g.move(offsetX,offsetY);

	}
}
function UserUp(e){
	var rect_rect = G.rect_rect;

	if( cup30g.isMove ){

		console.log('30g up');

		cup30g.up();

		// 是否与 70g 杯子相撞
		if(rect_rect(cup30g.x,cup30g.y,cup30g.w,cup30g.h,cup70g.x,cup70g.y,cup70g.w,cup70g.h)){

			console.log('30g 与 70g 杯子相撞');

			cup30gPourToCup70g();

		}
		// 是否与 100g 杯子相撞
		else if(rect_rect(cup30g.x,cup30g.y,cup30g.w,cup30g.h,cup100g.x,cup100g.y,cup100g.w,cup100g.h)){

			console.log('30g 与 100g 杯子相撞');

			cup30gPourToCup100g();

		}
		// 无相撞
		else{

			mask_layer.show();

			cup30g.tweenAnim(79,360,function(){
				mask_layer.hide();
			});

		}

	}else if( cup70g.isMove ){

		console.log('70g up');

		cup70g.up();

		// 是否与 30g 杯子相撞
		if(rect_rect(cup30g.x,cup30g.y,cup30g.w,cup30g.h,cup70g.x,cup70g.y,cup70g.w,cup70g.h)){

			console.log('70g 与 30g 杯子相撞');

			cup70gPourToCup30g();

		}
		// 是否与 100g 杯子相撞
		else if(rect_rect(cup70g.x,cup70g.y,cup70g.w,cup70g.h,cup100g.x,cup100g.y,cup100g.w,cup100g.h)){

			console.log('70g 与 100g 杯子相撞');

			cup70gPourToCup100g();

		}
		// 无相撞
		else{

			mask_layer.show();

			cup70g.tweenAnim(366,312,function(){
				mask_layer.hide();
			});
			
		}

	}else if( cup100g.isMove ){

		console.log('100g up');

		cup100g.up();

		// 是否与 30g 杯子相撞
		if(rect_rect(cup30g.x,cup30g.y,cup30g.w,cup30g.h,cup100g.x,cup100g.y,cup100g.w,cup100g.h)){

			console.log('100g 与 30g 杯子相撞');

			cup100gPourToCup30g();

		}
		// 是否与 70g 杯子相撞
		else if(rect_rect(cup70g.x,cup70g.y,cup70g.w,cup70g.h,cup100g.x,cup100g.y,cup100g.w,cup100g.h)){

			console.log('100g 与 70g 杯子相撞');

			cup100gPourToCup70g();
			
		}
		// 无相撞
		else{

			mask_layer.show();

			cup100g.tweenAnim(652,233,function(){
				mask_layer.hide();
			});
			
		}

	}
}
/**
 * 倒水动画
 */
function cup30gPourToCup70g(){ // 30g杯子 倒入 70g杯子
	var daoru = getWaterGraduate(cup30g,cup70g,79,360);

	if( !daoru ){
		return ;
	}

	mask_layer.show();
	cup30g.tweenAnim(500,250,function(){
		if( cup30g.graduate == daoru ){
			// 完全倒入
			cup30g.pourComplete(cup70g,function(){
				cup30g.tweenAnim(79,360,function(){
					mask_layer.hide();
				});
			});
		}else{
			// 未完全倒入
			cup30g.unPourComplete(daoru,cup70g,function(){
				cup30g.tweenAnim(79,360,function(){
					mask_layer.hide();
				});
			});
		}
	});
}
function cup30gPourToCup100g(){ // 30g杯子 倒入 100g杯子
	var daoru = getWaterGraduate(cup30g,cup100g,79,360);

	if( !daoru ){
		return ;
	}

	mask_layer.show();
	cup30g.tweenAnim(786,180,function(){
		if( cup30g.graduate == daoru ){
			// 完全倒入
			cup30g.pourComplete(cup100g,function(){
				cup30g.tweenAnim(79,360,function(){
					mask_layer.hide();
				});
			});
		}else{
			// 未完全倒入
			cup30g.unPourComplete(daoru,cup100g,function(){
				cup30g.tweenAnim(79,360,function(){
					mask_layer.hide();
				});
			});
		}
	});
}

function cup70gPourToCup30g(){ // 70g杯子 倒入 30g杯子
	var daoru = getWaterGraduate(cup70g,cup30g,366,312);

	if( !daoru ){
		return ;
	}

	mask_layer.show();
	cup70g.tweenAnim(213,300,function(){
		if( cup70g.graduate == daoru ){
			// 完全倒入
			cup70g.pourComplete(cup30g,function(){
				cup70g.tweenAnim(366,312,function(){
					mask_layer.hide();
				});
			});
		}else{
			// 未完全倒入
			cup70g.unPourComplete(daoru,cup30g,function(){
				cup70g.tweenAnim(366,312,function(){
					mask_layer.hide();
				});
			});
		}
	});
}
function cup70gPourToCup100g(){ // 70g杯子 倒入 100g杯子
	var daoru = getWaterGraduate(cup70g,cup100g,366,312);

	if( !daoru ){
		return ;
	}

	mask_layer.show();
	cup70g.tweenAnim(786,173,function(){
		if( cup70g.graduate == daoru ){
			// 完全倒入
			cup70g.pourComplete(cup100g,function(){
				cup70g.tweenAnim(366,312,function(){
					mask_layer.hide();
				});
			});
		}else{
			// 未完全倒入
			cup70g.unPourComplete(daoru,cup100g,function(){
				cup70g.tweenAnim(366,312,function(){
					mask_layer.hide();
				});
			});
		}
	});
}

function cup100gPourToCup30g(){ // 100g杯子 倒入 30g杯子
	var daoru = getWaterGraduate(cup100g,cup30g,652,233);

	if( !daoru ){
		return ;
	}

	mask_layer.show();
	cup100g.tweenAnim(213,300,function(){
		if( cup100g.graduate == daoru ){
			// 完全倒入
			cup100g.pourComplete(cup30g,function(){
				cup100g.tweenAnim(652,233,function(){
					mask_layer.hide();
				});
			});
		}else{
			// 未完全倒入
			cup100g.unPourComplete(daoru,cup30g,function(){
				cup100g.tweenAnim(652,233,function(){
					mask_layer.hide();
				});
			});
		}
	});
}
function cup100gPourToCup70g(){ // 100g杯子 倒入 70g杯子
	// var xu = 70 - cup70g.graduate,graduate = cup100g.graduate,daoru;
	// // 判断 被倒入水的杯子满的情况 或者 倒入杯子为零的情况
	// if( (xu == 0) || (graduate == 0) ){
	// 	cup100g.tweenAnim(652,233);
	// 	return;
	// }else{
	// 	// 判断 倒入杯子 大于等于 被倒入杯子需要的水
	// 	if( graduate >= xu ){
	// 		daoru = xu;
	// 	}
	// 	// 判断 倒入杯子 小于 被倒入杯子需要的水
	// 	else{
	// 		daoru = graduate;
	// 	}
	// }
	
	var daoru = getWaterGraduate(cup100g,cup70g,652,233);

	if( !daoru ){
		return ;
	}

	mask_layer.show();

	cup100g.tweenAnim(500,250,function(){
		if( cup100g.graduate == daoru ){
			// 完全倒入
			cup100g.pourComplete(cup70g,function(){
				cup100g.tweenAnim(652,233,function(){
					mask_layer.hide();
				});
			});
		}else{
			// 未完全倒入
			cup100g.unPourComplete(daoru,cup70g,function(){
				cup100g.tweenAnim(652,233,function(){
					mask_layer.hide();
				});
			});
		}
	});
}

// 计算出需要倒入的水量
// cup1 倒入杯子 cup2 被倒入杯子
function getWaterGraduate(cup1,cup2,x,y){
	var xu = cup2.fillGraduate - cup2.graduate,
	graduate = cup1.graduate,daoru;
	// 判断 被倒入水的杯子满的情况 或者 倒入杯子为零的情况
	if( (xu == 0) || (graduate == 0) ){
		cup1.tweenAnim(x,y);
		return false;
	}else{
		// 判断 倒入杯子 大于等于 被倒入杯子需要的水
		if( graduate >= xu ){
			daoru = xu;
		}
		// 判断 倒入杯子 小于 被倒入杯子需要的水
		else{
			daoru = graduate;
		}
	}
	return daoru;
}